<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      function f(num = 1) {
        return new Promise((resolve) => {
          setTimeout(() => {
            resolve(num * 3);
          }, 1000);
        });
      }
      function* fn1() {
        const num1 = yield f(1);
        const num2 = yield f(num1);
        const num3 = yield f(num2);
        return num3;
      }
      function generatorAsync(fn) {
        return function () {
          return new Promise((resolve,reject) => {
            const g = fn.apply(this,arguments);
            // 封装一个函数，next的函数
            function go(key,val) {
              let res
              try {
                res = g[key](val)
              } catch (error) {
                return reject(error)
              }
              const {value,done} = res
              if(done) {
                return resolve(value)
              }
              value.then(res=>console.log(res))
              return value.then(res=>{go('next',res),err=>{go('throw',err)}})
            }
            go('next') //第一次执行
          //   const res = g.next();
          //   res.value.then((res1) => {
          //     console.log(res1);
          //     const num1 = g.next(res1);
          //     num1.value.then((res) => {
          //       console.log(res);
          //       const num2 = g.next(res);
          //       num2.value.then((res) => {
          //         console.log(res);
          //         resolve(g.next(res).value)                  
          //       });
          //     });
          //   });
          });
        };
      }
      const asyncFn = generatorAsync(fn1)
      asyncFn().then(res=>{
        console.log(res);
      })
      // 等同于async和await
      async function asyncF() {
        let num1 = await f(1)
        let num2 = await f(num1)
        let num3 = await f(num2)
        return num3
      } 
      // asyncF().then(res=>{
      //   console.log(res);
      // })
      console.log(Number.MAX_SAFE_INTEGER);
      console.log(2**53-1);
    </script>
  </body>
</html>
